import axios from "axios";
import { Toast } from "vant";
import store from "@/store/index.js"
import router from"@/router/index.js"
// 创建实例
const instance = axios.create({
    baseURL: 'http://api.w0824.com/api/',
    timeout: 10000,
    
  });
 
  // 添加请求拦截器
instance.interceptors.request.use(function (config) {
   if(store.state.token){
     config.headers['token'] = store.state.token;
   }
   config.headers['If-Modified-Since'] = 0;
   config.headers['Cache-Control'] ='no-store';
    // 在发送请求之前做些什么
    // console.log('请求拦截器');
    Toast.loading({
        message:"加载中...",
        forbidClick:true,
    })
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

 
// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // console.log('响应拦截器');
     //数据出现立刻关闭
    Toast.clear();
    if(response.data.status === 40001){
      store.commit('setToken','')
      Toast(response.data.message)
      router.replace({
        path:"/login",
        query:{
          redirect: router.currentRoute.fullPath
        }
      })
    }
    return response.data;
  },function(error){
    Toast.clear()
    return Promise.reject(error);
  });
 
  // 导出实例
  export default instance
  